package 其他.华为;

import java.util.*;

public class Test2 {
    public static void main(String[] args) {
        /*
            student subClassOf person
            Tom instanceOf student
            Marry instanceOf person

            Marry,Tom
         */
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        UnionFind u= new UnionFind();
        ArrayList<String> instances = new ArrayList<>();
        //构造初始uf[]
        for(int i=0;i<n;i++){
            String temp = sc.nextLine();
            String [] splits = temp.split(" ");
            if(splits[1].equals("subClassOf")){
                u.union(splits[0],splits[2]);
            }
            if(splits[1].equals("instanceOf")){
                instances.add(splits[0]);
                u.union(splits[0],splits[2]);
            }
        }
        //连接
        ArrayList<String> res = new ArrayList<>();
        String find = sc.next();
        for(String s:instances){
            if(u.check(s,find)){
                res.add(s);
            }
        }
        //输出要求字典序(首字母排序
        Collections.sort(res);
        if(res.size()==0){
            System.out.println("empty");
        }else {
            StringBuilder sb=new StringBuilder();
            for (int i=0;i<res.size()-1;i++) {
                sb.append(res.get(i)+" ");
            }
            sb.append(res.get(res.size()-1));
            System.out.println(sb.toString());
        }
    }

    static class UnionFind{
        Map<String, String> map = new HashMap<>();
        UnionFind(){

        }
        boolean check(String s , String find){
            //就是while迭代，他写个递归
            if(map.get(s).equals(s) && !s.equals(find)){
                return false;
            }
            if(map.get(s).equals(find)){
                return true;
            }
            return check(map.get(s),find);
        }
        void union(String s1, String s2){
            String root1= find(s1);
            String root2= find(s2);
            if(!root1.equals(root2)){
                map.put(s1,s2);
            }
        }

        String find(String s){
            //我抽出到构造函数
            if(!map.containsKey(s)){
                map.put(s,s);
            }

            if(!s.equals(map.get(s))){
                map.put(s,find(map.get(s)));
            }
            return map.get(s);
        }
    }
}
